package binaryTree.basic;

public class BinaryTreeLevel {
    // 递归求二叉树每个节点所在的层数
    public static void getLevel(TreeNode root, int level) {
        if (root == null) {
            return;
        }
        System.out.println(root.val + " level: " + level);
        getLevel(root.left, level + 1);
        getLevel(root.right, level + 1);
    }
    public static void main(String[] args) {
        // 创建二叉树
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        // 求二叉树每个节点所在的层数
        getLevel(root, 1);
    }
}
